package com.system.enterprisemanagementsystem.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.system.enterprisemanagementsystem.exception.BusinessException;
import com.system.enterprisemanagementsystem.mapper.StudentMapper;
import com.system.enterprisemanagementsystem.pojo.Clazz;
import com.system.enterprisemanagementsystem.pojo.PageResult;
import com.system.enterprisemanagementsystem.pojo.Student;
import com.system.enterprisemanagementsystem.pojo.StudentQueryParam;
import com.system.enterprisemanagementsystem.service.StudentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.List;

@Slf4j
@Service
public class StudentServiceImpl implements StudentService {

    private final StudentMapper studentMapper;

    public StudentServiceImpl(StudentMapper studentMapper) {
        this.studentMapper = studentMapper;
    }

    @Override
    public PageResult page(StudentQueryParam studentQueryParam) {
        // 1. 设置分页参数
        PageHelper.startPage(studentQueryParam.getPage(), studentQueryParam.getPageSize());

        // 2. 执行查询
        List<Student> studentList = studentMapper.selectByCondition(studentQueryParam);

        // 3. 封装分页结果
        Page<Student> p = (Page<Student>) studentList;
        return new PageResult(p.getTotal(), p.getResult());
    }

    @Override
    public Student getStudentById(int id) {
        return studentMapper.getStudentById(id);
    }

    @Override
    public void updateStudent(Student student) {

    studentMapper.updateStudent(student);

    }

    @Override
    public void deleteStudentsByIds(List<Integer> ids) {
        if (ids == null || ids.isEmpty()) {
            throw new BusinessException("请选择要删除的学员");
        }

        // 执行批量删除
        int affectedRows = studentMapper.deleteByIds(ids);

        if (affectedRows == 0) {
            throw new BusinessException("删除失败，未找到对应的学员");
        }

        if (affectedRows < ids.size()) {
            log.warn("批量删除部分成功: 请求删除 {} 条，实际删除 {} 条", ids.size(), affectedRows);
        }

        log.info("批量删除学员成功, 删除数量: {}", affectedRows);
    }

    @Override
    public void handleViolation(Integer studentId, Integer score) {
        // 1. 检查学员是否存在
        Student student = studentMapper.getStudentById(studentId);
        if (student == null) {
            throw new BusinessException("学员不存在");
        }

        // 2. 更新违纪次数和分数
        student.setViolationCount((short) (student.getViolationCount() + 1));
        student.setViolationScore((short) (student.getViolationScore() + score));

        // 3. 执行更新
        int affectedRows = studentMapper.updateStudentViolation(student);
        if (affectedRows == 0) {
            throw new BusinessException("更新失败");
        }

        log.info("学员违纪记录更新成功, 学员: {}, 累计扣分: {}", student.getName(), student.getViolationScore());
    }

    @Override
    public void addStudent(Student student) {
        // 可添加数据校验逻辑，例如手机号、学号唯一性
        studentMapper.addStudent(student);
    }


}
